/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tolls;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.swing.JOptionPane;
import model.DBManager;

/**
 *
 * @author Aggelos
 */
public class PaymentForm extends javax.swing.JFrame {
    
    EntityManager em;
    String cardId;
    String regNum;
    String balance;
    double total;
    /**
     * Creates new form PaymentForm
     */
    public PaymentForm() {
        this.em = DBManager.em;
        initComponents();
    }
    
    public PaymentForm(EntityManager em, String strCardId,String strRegNum,String strBalance){
        this.em = em;
        initComponents();
        this.regNum=strRegNum;
        this.cardId=strCardId;
        this.balance=strBalance;
        cardjtf.setText(strCardId); 
        regNumjtf.setText(strRegNum);
        balancejtf.setText(strBalance);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cardIdLabel = new javax.swing.JLabel();
        cardjtf = new javax.swing.JTextField();
        regNumLabel = new javax.swing.JLabel();
        regNumjtf = new javax.swing.JTextField();
        button1 = new javax.swing.JButton();
        balanceLabel = new javax.swing.JLabel();
        balancejtf = new javax.swing.JTextField();
        paymentLabel = new javax.swing.JLabel();
        paymentjtf = new javax.swing.JTextField();
        newBalanceLabel = new javax.swing.JLabel();
        newBalancejtf = new javax.swing.JTextField();
        button2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        cardIdLabel.setText("Αρ. Κάρτας : ");

        cardjtf.setEditable(false);

        regNumLabel.setText("Αρ. Πινακίδας : ");

        regNumjtf.setEditable(false);

        button1.setText("Πληρωμή");
        button1.setEnabled(false);
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });

        balanceLabel.setText("Υπόλοιπο : ");

        balancejtf.setEditable(false);

        paymentLabel.setText("Ποσό Πληρωμής :  ");

        paymentjtf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                paymentjtfActionPerformed(evt);
            }
        });

        newBalanceLabel.setText("Νέο Υπόλοιπο : ");

        button2.setText("Έλεγχος Πληρωμής");
        button2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(button1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newBalanceLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(newBalancejtf, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(balanceLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(balancejtf, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cardIdLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cardjtf, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(paymentLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(paymentjtf, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(regNumLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(regNumjtf, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(button2))))
                .addContainerGap(36, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cardIdLabel)
                    .addComponent(cardjtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(regNumLabel)
                    .addComponent(regNumjtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(balanceLabel)
                    .addComponent(balancejtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(paymentLabel)
                    .addComponent(paymentjtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(button2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newBalanceLabel)
                    .addComponent(newBalancejtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(47, 47, 47)
                .addComponent(button1)
                .addContainerGap(68, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void button2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button2ActionPerformed
        // TODO add your handling code here:
        double payment = Double.parseDouble(paymentjtf.getText());
        if (payment < 10){
            JOptionPane.showMessageDialog(this,"Εισάγετε ποσό μεγαλύτερο των 10 ευρώ!");
        }
        else{
            double balance1 = Double.parseDouble(balance);
            total = payment + balance1;
            newBalancejtf.setText(total + "");
            button1.setEnabled(true);
        }
    }//GEN-LAST:event_button2ActionPerformed

    private void paymentjtfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_paymentjtfActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_paymentjtfActionPerformed

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
        // TODO add your handling code here:
        
        int id = Integer.parseInt(this.cardId);
        Date date = new Date();
        java.sql.Date sqlDate = new java.sql.Date(date.getTime());

        em.createNativeQuery("INSERT INTO PAYMENT(CARD_ID,PAYMENT_TIMESTAMP,OLD_BALANCE) VALUES(" + cardId + ", '" + sqlDate + "', " + total + ")").executeUpdate();
        em.getTransaction().commit();
        JOptionPane.showMessageDialog(this,"Η πληρωμή ολοκληρώθηκε!");
        
        dispose();
    }//GEN-LAST:event_button1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel balanceLabel;
    private javax.swing.JTextField balancejtf;
    private javax.swing.JButton button1;
    private javax.swing.JButton button2;
    private javax.swing.JLabel cardIdLabel;
    private javax.swing.JTextField cardjtf;
    private javax.swing.JLabel newBalanceLabel;
    private javax.swing.JTextField newBalancejtf;
    private javax.swing.JLabel paymentLabel;
    private javax.swing.JTextField paymentjtf;
    private javax.swing.JLabel regNumLabel;
    private javax.swing.JTextField regNumjtf;
    // End of variables declaration//GEN-END:variables
}
